$(function(){
	
});
function editStyler(value,row,index){
	//if (value < 30){
		return 'background-color:#D8F777;';//color:red;';
	//}
}
function loadColumn(r){
	var tablename = r.name;
	$("#tablecomments").html(r.comments)
	$.ajax({
	     type: 'post',
	     dataType:"json",  
	     contentType : 'application/json;charset=utf-8',
	     url: "queryColumns", 
	     data: JSON.stringify({"tablename":tablename}),   
	     success: function (data) {
	    	 
	    	 $(data).each(function(index,element){
	    		 var row = data[index];
	    		 var datatype = row.datatype;
	    		 row.ispersistence ='Y';
	    		 row.issearch ='N';
	    		 row.isshow ='Y';
	    		 
	    		 row.propertyname = row.name.transform();
	    		 switch(datatype)
	    		 {
	    		 case "VARCHAR2":
	    			 row.javatype="String";
	    		   break;
	    		 case "NUMBER":
	    			 row.javatype="Long";
	    		   break;
	    		 case "DATE":
	    			 row.javatype="Date";
	    		   break;
	    		 default:
	    			 row.javatype="String"
	    		 }
	    	 });
	    	 $("#columnList").datagrid("loadData",data)
		 },
		 error: function (msg) {
			$.messager.progress('close');
	    	$.messager.alert('提示', '链接异常')
	     }
	});
}


function loadSql(){
	
	var sql = $("#sql").textbox("getValue");
	$.ajax({
	     type: 'post',
	     dataType:"json",  
	     contentType : 'application/json;charset=utf-8',
	     url: "loadSql", 
	     data: JSON.stringify({"sql":sql}),   
	     success: function (data) {
	    	 $(data).each(function(index,element){
	    		 var row = data[index];
	    		 var datatype = row.datatype;
	    		 row.issearch ='N';
	    		 row.isshow ='Y';
	    		 try {
	    			// str = str.match(/(\S.)name/)[1];
		    		 sql = sql.toUpperCase();
		    		 pattern1 = new RegExp("(\\S*)"+"[ ]*" +row.name); 
		    		 var pre =  sql.match(pattern1)[1];
		    		 if(pre.match(/\.\S/)){
		    			 row.searchname=pre
		    		 }else{
		    			 row.searchname=pre+row.name;
		    		 }
		    		 
				} catch (e) {
					
				}
	    		
	    		 row.propertyname = row.name.transform();
	    		 switch(datatype)
	    		 {
	    		 case "VARCHAR2":
	    			 row.javatype="String";
	    		   break;
	    		 case "NUMBER":
	    			 row.javatype="Long";
	    		   break;
	    		 case "DATE":
	    			 row.javatype="Date";
	    		   break;
	    		 default:
	    			 row.javatype="String"
	    		 }
	    	 });
	    	 $("#columnList").datagrid("loadData",data)
	    	
		 },
		 error: function (msg) {
			$.messager.progress('close');
	    	$.messager.alert('提示', '链接异常')
	     }
	});
	
	
}


var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){return true}
	if ($('#columnList').datagrid('validateRow', editIndex)){
		//var ed = $('#columnList').datagrid('getEditor', {index:editIndex,field:'productid'});
		//var productname = $(ed.target).combobox('getText');
		//$('#columnList').datagrid('getRows')[editIndex]['productname'] = productname;
		$('#columnList').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
function onClickRow(index){
	if (editIndex != index){
		if (endEditing()){
			$('#columnList').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
			editIndex = index;
		} else {
			$('#columnList').datagrid('selectRow', editIndex);
		}
	}
}
function append(){
	if (endEditing()){
		//$('#columnList').datagrid('appendRow',{status:'P'});
		editIndex = $('#columnList').datagrid('getRows').length-1;
		$('#columnList').datagrid('selectRow', editIndex)
				.datagrid('beginEdit', editIndex);
	}
}
function removeit(){
	if (editIndex == undefined){return}
	$('#columnList').datagrid('cancelEdit', editIndex)
			.datagrid('deleteRow', editIndex);
	editIndex = undefined;
}
function accept(){
	if (endEditing()){
		$('#columnList').datagrid('acceptChanges');
	}
}
function reject(){
	$('#columnList').datagrid('rejectChanges');
	editIndex = undefined;
}
function getChanges(){
	var rows = $('#columnList').datagrid('getChanges');
	alert(rows.length+' rows are changed!');
}

function oprow(i,row,index){
	
	return '<button onclick="moveup('+index+')">上移</button>'+'<button onclick="movedown('+index+')">下移</button>';
}
function moveup(indexup){
	var rows=$('#columnList').datagrid('getRows');  
	var rowlength=rows.length; 
	var selectrow=$('#columnList').datagrid('getRows')[indexup];  	
	
	if(indexup==0){  
        $.messager.alert('提示', '顶行无法上移!', 'warning');  
    }else{  
        $('#columnList').datagrid('deleteRow', indexup);//删除一行  
        indexup--;  
        $('#columnList').datagrid('insertRow', {  
            index:indexup,  
            row:selectrow  
        });  
        $('#columnList').datagrid('selectRow', indexup);  
    }  
}
function movedown(indexdown){
	var rows=$('#columnList').datagrid('getRows');  
	var rowlength=rows.length; 
	var selectrow=$('#columnList').datagrid('getRows')[indexdown]; 
	
	
	if(indexdown==rowlength-1){  
        $.messager.alert('提示', '底行无法下移!', 'warning');  
    }else{  
        $('#columnList').datagrid('deleteRow', indexdown);//删除一行  
        indexdown++;  
        $('#columnList').datagrid('insertRow', {  
            index:indexdown,  
            row:selectrow  
        });  
        $('#columnList').datagrid('selectRow', indexdown);  
    }  
} 

//table;
//columns;
//singleConfig;
function generatorSingle(){
	
	if(!$('#gcform').form('enableValidation').form('validate')){
		$.messager.alert('提示', '信息不完整！');
		return;
	}
	 $.messager.confirm("提示", "你确定要 生成码?", function (r) {
        if (r) {
        	$.messager.progress({ title:'请等待', msg:'正在保存...' });   
        	$.ajax({
        	     type: 'post',
        	     dataType:"json",  
        	     contentType : 'application/json;charset=utf-8', //设置请求头信息
        	     url: "generatorSingle", 
        	     data: JSON.stringify({"singleConfig":$("#gcform").serializeJson(),"columns": $("#columnList").datagrid("getRows")}),   
        	     success: function (data) {
        	    	 $.messager.progress('close');
        	    	 $.messager.alert('提示', '生成成功!!!')
        	    	 //data.filePath
        	    	$("#resultSrc").html(data.filePath)
        		 },
        		 error: function (msg) {
        			$.messager.progress('close');
			    	$.messager.alert('提示', '链接异常')
        	     }

        	});
		 
        }
    });
	
}


//table;
//columns;
//singleConfig;
function generatorSearchEntity(){
	
	if(!$('#gcform').form('enableValidation').form('validate')){
		$.messager.alert('提示', '信息不完整！');
		return;
	}
	 $.messager.confirm("提示", "你确定要 生成码?", function (r) {
      if (r) {
      	$.messager.progress({ title:'请等待', msg:'正在保存...' });   
      	$.ajax({
      	     type: 'post',
      	     dataType:"json",  
      	     contentType : 'application/json;charset=utf-8', //设置请求头信息
      	     url: "generatorSearchEntity", 
      	     data: JSON.stringify({"singleConfig":$("#gcform").serializeJson(),"columns": $("#columnList").datagrid("getRows")}),   
      	     success: function (data) {
      	    	 $.messager.progress('close');
      	    	 $.messager.alert('提示', '生成成功!!!')
      	    	 //data.filePath
      	    	$("#resultSrc").html(data.filePath)
      		 },
      		 error: function (msg) {
      			$.messager.progress('close');
			    	$.messager.alert('提示', '链接异常')
      	     }

      	});
		 
      }
  });
	
}